package com.liwenfeng.iostream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileInputStreamTest {
    public static void main(String[] args) {
        //创建文件字节输入流管道，与源文件接通。
        //1.一个一个字节读取：
//        FileInputStream fis = null;
//        int read;
//        try {
//            fis = new FileInputStream("D:\\code\\java\\JAVA22\\function2framework\\src\\main\\java\\com\\liwenfeng\\test.txt");
//            while ((read = fis.read()) != -1) {
//                System.out.print((char) read);
//            }
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        } finally {
//            if (fis != null) {
//                try {
//                    fis.close();
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }

        //2.一次性读整个文件到字节数组中
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("D:/code/java/JAVA22/function2framework/src/main/java/com/liwenfeng/test.txt");
            byte[] bytes = fis.readAllBytes();
            System.out.println(new String(bytes));
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        //文件字节输出流
        FileOutputStream fos=null;
        try {
            fos = new FileOutputStream("D:/code/java/JAVA22/function2framework/src/main/java/com/liwenfeng/test.txt", true);
            fos.write("\r\n".getBytes());
            fos.write("我爱你中国".getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fos!=null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}
